package com.bidv.wcm;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import com.ibm.workplace.wcm.api.Content;
import com.ibm.workplace.wcm.api.DocumentId;
import com.ibm.workplace.wcm.api.DocumentIdIterator;
import com.ibm.workplace.wcm.api.DocumentLibrary;
import com.ibm.workplace.wcm.api.DocumentTypes;
import com.ibm.workplace.wcm.api.Repository;
import com.ibm.workplace.wcm.api.VirtualPortalScopedAction;
import com.ibm.workplace.wcm.api.WCM_API;
import com.ibm.workplace.wcm.api.Workspace;
import com.ibm.workplace.wcm.api.exceptions.WCMException;

public class VPList implements VirtualPortalScopedAction{

	
	public String Data = "";
	public String Dir = "";
	public int Npp = 0;
	public int PageNum = 0;
	public String cp = "";
	public String context = "";
	public String doclib = "";
	public String index = "0";
	public StringBuilder sb2 = new StringBuilder();
	public Vector<String> v = new Vector<String>();
	public Vector<String> v2 = new Vector<String>();
	//public constructor
	public VPList(){
	}


	@Override
	public void run() throws WCMException {
		Repository repo = WCM_API.getRepository();
		Workspace myWorkspace = repo.getSystemWorkspace();
		myWorkspace.login();
		DocumentLibrary docLib = myWorkspace.getDocumentLibrary("TrangChuWCM");
		myWorkspace.setCurrentDocumentLibrary(docLib);
		
	    DocumentId<?> site = myWorkspace.findAllByPath("/TrangChuWCM/",
				DocumentTypes.SiteArea,
				Workspace.WORKFLOWSTATUS_PUBLISHED,
				new DocumentLibrary[] { myWorkspace.getDocumentLibrary(doclib) }
				).next();
	    
	    DocumentId<?> newsAT = myWorkspace.findAllByPath("VanBanNoiBat",
				DocumentTypes.AuthoringTemplate,
				Workspace.WORKFLOWSTATUS_PUBLISHED,
				new DocumentLibrary[] { myWorkspace.getDocumentLibrary("TrangChuWCM") }
				).next();
    
	    DocumentIdIterator<Content> docContent = myWorkspace.contentSearch(newsAT, new DocumentId[] {site}, null, null, false, false, false, Workspace.SORT_KEY_PUBLISH_DATE,null,null,false);
	    StringBuilder sb = new StringBuilder();
	    
	    int k = 0;
	    int k1 = 0;
	    int k2 = 0;
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		while (docContent.hasNext()) {
			DocumentId<Content> contentid = docContent.next();
			k++;
			if (k>Npp*(PageNum-1) && k<=Npp*PageNum){
				Content contentc = myWorkspace.getById(contentid);
				String id = contentid.getId();
				String label = contentc.getTitle()+" ("+df.format(contentc.getDateEnteredStage())+")";
				if (index.equals("0")){
					//String content = "Content";
					//String url = cp+"/GetContent?ID="+id+"&Context="+context+"&DocLib="+doclib;
					//sb.append(Utils.getScript(label, url, content));
				} else {
					v.add(label);
					v2.add(id);
				}
				sb.append("<br>");
			} else if (k<=Npp*(PageNum-1)) {
				k1++;
			} else {
				k2++;
			}
		}
		int p1 = k1/Npp;
		int p2 = k2/Npp;
		if (k2 % Npp >0){
			p2++;
		}
		for (int i=1;i<=p1;i++){
			setPageLink(String.valueOf(i));
		}
		
		sb2.append("<b>"+String.valueOf(PageNum)+"</b>");
		sb2.append("  &nbsp");
		
		for (int i=1;i<=p2;i++){
			setPageLink(String.valueOf(i+p1+1));
		}
		Data = sb.toString()+"<br>"+sb2.toString();
	}
	
	public void setPageLink(String label){
		String content= "ListContent";
		String url = cp+"/GetList?Dir="+Dir+"&Npp="+Npp+"&PageNum="+label+"&Context="+context+"&DocLib="+doclib+"&index=0";
		sb2.append(Utils.getScript2(label, url , content));
		sb2.append("  &nbsp");
	}
}